<?php
//活动表

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Casts\Attribute;

class MoneyLogModel extends Model
{
    use HasFactory, SoftDeletes;


    protected $table = 'money_log';
    const BANK_IN = 'bank_in';
    const BANK_OUT = 'bank_out';
    const BANK_INTEREST = 'bank_interest';
    const STOCK_EARN = 'stock_earn';
    const STOCK_LOST = 'stock_lost';

    const TYPES = [
        SELF::BANK_IN => '银行转入',
        SELF::BANK_OUT => '银行转出',
        SELF::BANK_INTEREST => '银行利息',
        SELF::STOCK_EARN => '股票盈利',
        SELF::STOCK_LOST => '股票亏损'
    ];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    // 访问器
    protected function money(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }

    protected function total(): Attribute
    {
        return new Attribute(
            get: fn ($value) => round($value / 100, 2),
            set: fn ($value) => $value * 100,
        );
    }
}
